﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace B13_Ex01_David_300512340_Alexey_321203689
{
    public partial class AlbumForm : Form
    {
        public AlbumWrapper m_SelectedAlbum;
        private int m_Index = 0; // Index for the listBoxPictures, used when click on buttonSlideShow. 

        public AlbumForm()
        {
            InitializeComponent();
        }

        public AlbumForm(AlbumWrapper i_SelectedAlbum)
        {
            InitializeComponent();
            m_SelectedAlbum = i_SelectedAlbum;
            listBoxPictures.DataSource = m_SelectedAlbum.GetAlbumPhotosList();
        }

        private void listBoxPictures_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxPictures.SelectedItems.Count == 1)
            {
                PhotoWrapper selectedPhoto = listBoxPictures.SelectedItem as PhotoWrapper;
                
                pictureBoxFromAlbum.LoadAsync(selectedPhoto.GetPhotoURL);
            }
        }

        private void buttonSaveSelectedPhoto_Click(object sender, EventArgs e)
        {
            int imageCounter = 1; // If there is more than one photo, number the photos.
            string path;
            string photoName = textBoxPictueSaveName.Text == string.Empty ? "facebookImage" : textBoxPictueSaveName.Text;
            string fullPhotoName = listBoxPictures.SelectedItems.Count == 1 ?
                photoName : string.Format("{0}{1}", photoName, imageCounter);

            folderBrowserDialog.ShowDialog();
            path = folderBrowserDialog.SelectedPath; // Get the selected path.
            if (pathIsValid(path))
            {
                MessageBox.Show(null, "Saving photos, please wait.", "Save Photos", MessageBoxButtons.OK, MessageBoxIcon.Information);
                foreach (PhotoWrapper photo in listBoxPictures.SelectedItems)
                {
                    photo.SavePhoto(photo, path, fullPhotoName);
                    imageCounter++;
                    fullPhotoName = string.Format("{0}{1}", photoName, imageCounter);
                }

                MessageBox.Show(null, "Done saving photos.", "Save Photos", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonSaveAlbum_Click(object sender, EventArgs e)
        {
            string path;

            folderBrowserDialog.ShowDialog();
            path = folderBrowserDialog.SelectedPath;
            if (pathIsValid(path))
            {
                m_SelectedAlbum.SaveAlbum(m_SelectedAlbum, path, textBoxPictueSaveName.Text);
            }
        }

        private bool pathIsValid(string i_Path)
        {
            return i_Path != string.Empty;
        }

        private void listBoxPictures_MouseUp(object sender, MouseEventArgs e)
        {
            int point = listBoxPictures.IndexFromPoint(e.X, e.Y);
            PhotoWrapper item = point >= 0 ? listBoxPictures.Items[point] as PhotoWrapper : null; // If the album is empty, null.
            PhotoWrapper photo = null;

            if (listBoxPictures.SelectedItems.Contains(item))
            {
                photo = item;
                pictureBoxFromAlbum.LoadAsync(photo.GetPhotoURL);
            }
        }

        private void buttonSlideShow_Click(object sender, EventArgs e)
        {
            int timeSlide;

            if (listBoxPictures.Items.Count > 1)
            {
                try
                {
                    timeSlide = int.Parse(comboBoxSlideTime.Text) * 1000;
                }
                catch
                {
                    MessageBox.Show("Invalid input. Default interval 2 seconds will be used.", "Cant play", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    comboBoxSlideTime.Text = "2";
                    timeSlide = 2000;
                }
                timerSlideShow.Interval = timeSlide;
                timerSlideShow.Enabled = !timerSlideShow.Enabled;
                buttonSlideShow.Text = buttonSlideShow.Text == "Play slide show" ? "Stop slide show" : "Play slide show";
                buttonResetSlideShow.Enabled = !buttonResetSlideShow.Enabled;
            }
            else
            {
                MessageBox.Show("Only one picture in this album.", "Cant play", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
        }

        private void timerTick(object sender, EventArgs e)
        {
            PhotoWrapper photo;

            if (m_Index < listBoxPictures.Items.Count)
            {
                
                photo = listBoxPictures.Items[m_Index] as PhotoWrapper;
                pictureBoxFromAlbum.LoadAsync(photo.GetPhotoURL);
                listBoxPictures.ClearSelected();
                listBoxPictures.SelectedIndex = m_Index;
                m_Index++;
            }
            else
            {
                m_Index = 0;
                timerSlideShow.Stop();
            }
        }

        private void buttonResetSlideShow_Click(object sender, EventArgs e)
        {
            timerSlideShow.Stop();
            m_Index = 0;
            listBoxPictures.ClearSelected();
            listBoxPictures.SelectedIndex = m_Index;
        }
    }
}
